package kr.co.icebird.temp;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class AppletExam01 extends JApplet implements ActionListener{

	
	private Container con;
	private JTextField ip_tf = new JTextField();
	private JButton conn_bt = new JButton("접속");
	private JTextArea view_ta = new JTextArea();
	private JScrollPane view_jsp = new JScrollPane(view_ta);
	
	public void init(){
		con = this.getContentPane();
		con.setLayout(new BorderLayout());
		
		JPanel jp = new JPanel(new BorderLayout());
		
		jp.add("Conter", ip_tf);
		jp.add("Last", conn_bt);
		jp.setBorder(new TitledBorder("접속설정"));
		
		con.add("Width",jp);
		
		view_jsp.setBorder(new TitledBorder("결과 출력"));
		
		con.add("Center",view_jsp);
	}
	
	public void start(){
		ip_tf.addActionListener(this);
		conn_bt.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == ip_tf || e.getSource() == conn_bt){
			String ip = ip_tf.getText();
			
			try{
				URL url = new URL(ip);
				URLConnection uc = url.openConnection();
				
				uc.setDoInput(true);
				InputStreamReader isr = new InputStreamReader(uc.getInputStream());
				
				BufferedReader br = new BufferedReader(isr, 512);
				while(true){
					String s = br.readLine();
					if(s == null)break;
					view_ta.append(s+"\n");
				}
				br.close();
			}catch (Exception ex) {
				// TODO: handle exception
			}
		}
	}
}
